using System;
using System.Xml.Serialization;
using AndyPike.Unfuddle.Contracts;

namespace AndyPike.Unfuddle.Entities.Projects
{
    /// <summary>
    /// Represents an Unfuddle Project entity
    /// </summary>
    [XmlRoot("project")]
    public class Project : IEntity
    {
        /// <summary>
        /// The project id, this is read-only
        /// </summary>
        [XmlElement("id")]
        public int Id { get; set; }

        /// <summary>
        /// The project title, this is read-write
        /// </summary>
        [XmlElement("title")]
        public string Title { get; set; } 

        /// <summary>
        /// Can be one of the following values: blue, green, grey, orange, purple, red, teal
        /// </summary>
        [XmlElement("theme")]
        public ProjectThemes Theme { get; set; }

        /// <summary>
        /// The project description, this is read-write
        /// </summary>
        [XmlElement("description")]
        public string Description { get; set; }

        /// <summary>
        /// The project created date, this is read-only
        /// </summary>
        [XmlElement("created-at")]
        public DateTime Created { get; set; }

        /// <summary>
        /// The project last updated date, this is read-only
        /// </summary>
        [XmlElement("updated-at")]
        public DateTime Updated { get; set; }

        /// <summary>
        /// The id of the account that this project belows to, this is read-only
        /// </summary>
        [XmlElement("account-id")]
        public int AccountId { get; set; }

        /// <summary>
        /// Whether the project is archived, this is read-write
        /// </summary>
        [XmlElement("archived")]
        public bool Archived { get; set; }

        /// <summary>
        /// The amount of space in KB that this project takes up, this is read-only
        /// </summary>
        [XmlElement("disk-usage")]
        public int DiskUsageKB { get; set; }

        /// <summary>
        /// The project short name, this is read-only
        /// </summary>
        [XmlElement("short-name")]
        public string ShortName { get; set; }

        /// <summary>
        /// Can be one of the following values: reporter, none, nochange
        /// </summary>
        [XmlElement("assignee-on-resolve")]
        public ProjectResolveActions AssigneeOnResolve { get; set; }

        /// <summary>
        /// Whether tickets in this project close automatically on resolve, this is read-write
        /// </summary>
        [XmlElement("close-ticket-simultaneously-default")]
        public bool CloseTicketSimultaneouslyDefault { get; set; }

        /// <summary>
        /// The default report view for this project, this is read-write
        /// </summary>
        [XmlElement("default-ticket-report-id")]
        public int DefaultTicketReportId { get; set; }

        /// <summary>
        /// Whether time tracking is enabled for this project, this is read-write
        /// </summary>
        [XmlElement("enable-time-tracking")]
        public bool EnableTimeTracking { get; set; }

        /// <summary>
        /// Validates the project. The Title and ShortName properties must be provided. 
        /// The project Id must be 0 when creating or non-zero when updating.
        /// </summary>
        /// <param name="context">The entity validation context to use when checking property values</param>
        public virtual void Validate(EntityValidationContext context)
        {
            if(context == EntityValidationContext.Creating)
                GuardAgainst.EntityPropertyOutsideRange(this, "Id", Id, 0, 0);

            if (context == EntityValidationContext.Updating)
                GuardAgainst.EntityPropertyOutsideRange(this, "Id", Id, 1, int.MaxValue);

            GuardAgainst.EntityPropertyNullOrEmpty(this, "Title", Title);
            GuardAgainst.EntityPropertyNullOrEmpty(this, "ShortName", ShortName);
        }
    }
}